home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr50 / s3bas11.zip / S3BASTUT.DOC < prev    next >
Text File  |  1993-05-02  |  23KB  |  557 lines

  1. Subject..:      Low level Routines in Microsoft BASIC
  2. Author...:      George Spafford
  3. Version..:      1.1
  4. Date.....:      May 2, 1993
  5.  
  6. This small tutorial package is aimed at users who have a working knowledge
  7. of the Microsoft BASIC language.  It is not aimed at the novice programmer.
  8. I hope that you find the information in this tutorial useful.  If you do, please
  9. register this shareware.  As a programmer, you know that this type of an
  10. endeavor requires a substantial investment of time and effort.  If I am to
  11. continue it, I need your support.
  12.  
  13. Section Outline:
  14.  
  15.         1.      Getting Started
  16.                 a.      Binary Numbers
  17.                 b.      Hexidecimal Numbers
  18.                 c.      Decimal Numbers
  19.                 d.      Registers
  20.                 e.      Bit manipulations
  21.         2.      Interrupts
  22.         3.      Peeks
  23.         4.      Pokes
  24.         5.      Summary
  25.         6.      Revision History
  26.         7.      Please register this shareware!
  27.  
  28. -----------------------
  29. NOTES ABOUT SAMPLE CODE:
  30. -----------------------
  31.  
  32.         Look at the sample .BAS files that are included with this package.
  33.         Print them out and refer to them while you read this text.  The .BAS
  34.         files are heavily commented and should help to clarify some of your
  35.         questions.  If you are anything like me, looking at code clarifies my
  36.         understanding of a problem immensely.
  37.         
  38.         If you intend to use any of the sample programs after the 30 day
  39.         evaluation period, you must register the entire tutorial package.
  40.        
  41.  
  42. ----------------------- 
  43. 1.      GETTING STARTED
  44. -----------------------
  45.  
  46. I like BASIC!  While many people make excuses for using it, I find it a 
  47. pleasant language to develop in.  By adding libraries or writing my own
  48. add-ins, I can perform just about any task I need to.  As far as code
  49. reusability goes, if I used modularized programming with functions and
  50. subroutines, it is quite high.  Note, in order for a person to see most
  51. of the code that I wrote for the demonstration programs, I refrained from
  52. functions and subroutines in order to adopt a top-down approach.  The 
  53. exception to this is MAKEWORD& - which is a function that I used in several
  54. demos (I called it INT2LONG& in WORD.BAS).  Alright, let's get started.
  55.  
  56. A.      Binary Numbers
  57.  
  58. The binary number system reflects that a computer circuit can only be "on"
  59. or "off" - nothing else.  Thus, a number scheme is used to represent any
  60. number by means of a "1" or a "0".  Any number can be computed by using
  61. the equation:
  62.  
  63.         2^n + ... 2^3 + 2^2 + 2^1 + 2^0
  64.         
  65. Numbers, bit sequences are evaluated from right to left.  Let's take a look 
  66. at some examples of this:
  67.                           
  68.         8 Bits             Decimal
  69.         --------     =     -------
  70.         00000000                0        
  71.         00000001                1
  72.         00000010                2
  73.         00000011                3
  74.         00000100                4
  75.          ------
  76.         10000000              128
  77.         
  78.         8 Bit Positions
  79.         00000000
  80.         ||||||||      
  81.         ||||||||      Exponent  
  82.         ||||||||      --------
  83.         |||||||\-----    0
  84.         ||||||\------    1
  85.         |||||\-------    2
  86.         ||||\--------    3
  87.         |||\---------    4
  88.         ||\----------    5
  89.         |\-----------    6
  90.         \------------    7  
  91.         
  92.         Long Hand Examples:       
  93.         
  94.         01001101  = 0^7 + 2^6 + 0^5 + 0^4 + 2^3 + 2^2 + 0^1 + 2^0
  95.                   = 0   + 64  + 0   + 0   + 8   + 4   + 0   + 1
  96.                   = 77
  97.                   
  98.         10011100  = 2^7 + 0^6 + 0^5 + 2^4 + 2^3 + 2^2 + 0^1 + 0^0
  99.                   = 128 + 0   + 0   + 16  + 8   + 4   + 0   + 0
  100.                   = 156
  101.                   
  102.         00000111  = 0^7 + 0^6 + 0^5 + 0^4 + 0^3 + 2^2 + 2^1 + 2^0
  103.                   = 0   + 0   + 0   + 0   + 0   + 4   + 2   + 1
  104.                   = 7
  105.  
  106.         Note:   If a bit position is 0 then the value is 0 - why?
  107.                 The actual notation for bit calculation can be identified
  108.                 as (1 x 2^n) if a bit is on or (0 x 2^n) if a bit is off.
  109.                 I just skipped that particular step in my examples.
  110.                   
  111.         16 Bit Positions:
  112.         
  113.         00000000 00000000        Exponent
  114.         |||||||| ||||||||        --------
  115.         |||||||| |||||||\------     0
  116.         |||||||| ||||||\-------     1
  117.         |||||||| |||||\--------     2
  118.         |||||||| ||||\---------     3
  119.         |||||||| |||\----------     4
  120.         |||||||| ||\-----------     5
  121.         |||||||| |\------------     6
  122.         |||||||| \-------------     7
  123.         ||||||||
  124.         |||||||\---------------     8
  125.         ||||||\----------------     9
  126.         |||||\-----------------    10
  127.         ||||\------------------    11
  128.         |||\-------------------    12
  129.         ||\--------------------    13
  130.         |\---------------------    14
  131.         \----------------------    15
  132.         
  133.         A Long Hand Example:
  134.         
  135.         01000100 10010011
  136.         
  137.         =  0^15 + 2^14 + 0^13 + 0^12 + 0^11 + 2^10 + 0^9 + 0^8 + 2^7 + 0^6 +
  138.           +0^5  + 2^4  + 0^3  + 0^2  + 2^1  + 2^0
  139.                  
  140.         =  0    + 16384+ 0    + 0    + 0    + 1024 + 0   + 0   + 128 + 0   +
  141.           +0    + 16   + 0    + 0    + 2    + 1
  142.         
  143.         = 17,555  
  144.  
  145. B.      HEXADECIMAL NUMBERS
  146.  
  147.         The hexadecimal numbering system is a hold over from the days
  148.         when there were 4-bit processors.  If you have every studied
  149.         statistics, then you know if there are four available bit positions,
  150.         there are 16 possible bit combinations (2^4).  Thus, a base 16
  151.         number system was developed.
  152.  
  153.         Decimal        Hex      Binary
  154.         -------        ---      -------
  155.           0      =      0   =   0000
  156.           1             1       0001
  157.           2             2       0010
  158.           3             3       0011
  159.           4             4       0100
  160.           5             5       0101
  161.           6             6       0110
  162.           7             7       0111
  163.           8             8       1000
  164.           9             9       1001
  165.          10             A       1010
  166.          11             B       1011
  167.          12             C       1100
  168.          13             D       1101
  169.          14             E       1110
  170.          15             F       1111
  171.  
  172.         Since Hex is base 16, start from the right of a value and
  173.         work your way to the left - just as you did with binary.
  174.  
  175.         03F8  =  (0 * 16^3) + (3 * 16^2) + (F * 16^1) + (8 * 16^0)
  176.               =   0         +  3 * 256   +  15* 16    +  8 * 1
  177.               =   0         +  768       +  240       +  8
  178.               =   1,016
  179.  
  180.         2F    =  (2 * 16^1) + (F * 16^0)
  181.               =   32        +  15
  182.               =   47
  183.  
  184.         BASIC can process directly coded hex by prefixing the hex
  185.         value with "&H".
  186.  
  187.         &H03F8     <- to BASIC, this means 3F8 Hex, 1,016 decimal
  188.         
  189. C.      DECIMAL NUMBERS
  190.  
  191.         The decimal number system is what most of us are familiar with
  192.         so I will not go into great details about it.  BASIC, however,
  193.         does discriminate between declarations of numeric variables:
  194.  
  195.         Variable                                                    Memory
  196.         Suffix   Meaning                  Range                  Requirements
  197.         -------- ----------       ------------------------------ ------------
  198.         %        integer                 -32,768 to 32,767           (2 bytes)
  199.         &        long             -2,147,483,648 to 2,147,483,647    (4 bytes)
  200.         !        single precision                                    (4 bytes)
  201.         #        double precision                                    (8 bytes)
  202.  
  203. D.      REGISTERS
  204.  
  205.         Simply put, registers are memory locations within the processor.
  206.         
  207.         16 bit  8 bit   Usage
  208.         --      --      -----------------
  209.         AX      AL      Accumulator
  210.                 AH
  211.         BX      BL      Base
  212.                 BH
  213.         CX      CL      Count
  214.                 CH
  215.         DX      DL      Data  
  216.                 DH
  217.         DI              Destination index
  218.         SI              Source index
  219.         SP              Stack pointer
  220.         BP              Base pointer
  221.         DS              Data segment
  222.         CS              Code segment
  223.         ES              Extra segment
  224.         SS              Stack Segment
  225.         
  226.         AX, BX, CX and DX are 16-bit registers that can be further broken
  227.         down into two 8-bit registers each.  The Low register contains bits
  228.         0 to 7 and the High register contains bits 8 to 15.
  229.         
  230.         Flag Registers:
  231.         
  232.         Bit
  233.         Position        Definition
  234.         ----------      -------------------------------
  235.         0               Carry Flag
  236.         2               Parity Flag
  237.         4               Auxiliary Flag
  238.         6               Zero Flag
  239.         7               Sign Flag
  240.         8               Trap Flag
  241.         9               Interrupt Flag
  242.         A               Direction Flag
  243.         B               Overflow Flag
  244.         
  245.         Note:   You will see the Carry Flag frequently used to indicate
  246.                 whether an error has taken place or not.
  247.                 
  248. E.      BIT MANIPULATIONS
  249.  
  250.         Many of the functions at the machine level require that you operate
  251.         at the bit level rather than at the byte level.  While BASIC does
  252.         not offer a variable at the bit level, bits can be manipulated
  253.         through the use of the logical boolean operators.  Remember when
  254.         we discussed the bits earlier?  That 00000001 = 1 and so forth?
  255.         Consider the following, what is the fastest way to determine if
  256.         a number is odd or even?  Give up?  Take a look at this code fragment:
  257.         
  258.                 INPUT "Enter a number  ",number
  259.                 IF number AND 1 THEN
  260.                    PRINT "That is an odd number"
  261.                 ELSE   
  262.                    PRINT "That is an even number"
  263.                 END IF
  264.                 
  265.         The key in this fragment is the usage of the AND operator.
  266.         Take a look at these following odd numbers:
  267.         
  268.                 00000001 =      1
  269.                 00000011        3                                                  
  270.                 00000101        5
  271.                 00000111        7
  272.                 
  273.         What do they have in common?  They all have the right most bit
  274.         (bit 0) turned on.  The AND operator evaluates numbers and if
  275.         the number to the right of the and has the same bit(s) on as the
  276.         number to the left, then the answer is true.  
  277.         
  278.         Bits            Decimal
  279.         --------        -------
  280.         00000001          1
  281.         00000010          2
  282.         00000100          4
  283.         00001000          8
  284.         00010000         16
  285.         00100000         32
  286.         01000000         64
  287.         10000000        128
  288.         
  289.         Examples:
  290.         
  291.         96      AND     64      
  292.         01100000        01000000        TRUE    Bit 6 matches
  293.         
  294.         56      AND     8
  295.         00111000        00001000        TRUE    Bit 3 matches
  296.         
  297.         56      AND     2
  298.         00111000        00000010        FALSE   Bits do NOT match
  299.         
  300.         64      AND     8
  301.         01000000        00001000        FALSE   Bits do NOT match
  302.                 
  303.         Why does this come in handy?  Let's say you want to test
  304.         the FLAGS register after doing an INTERRUPT call in BASIC.
  305.         You want to know if it has been turned on due to an error:
  306.         
  307.         IF OutRegs.Flags AND 1  THEN
  308.            PRINT "Oh no, an error fried your hard drive!"
  309.         END IF  
  310.              
  311.         Let's say you use Crescent Software's excellent QuickPak
  312.         Pro library for DOS (one of my favorites).  Many of its
  313.         file information subroutines return a byte field that contains
  314.         the file's attributes.  How do you test the attributes?
  315.         DOS uses a byte to hold the information.  Its 8 bits are used
  316.         as follows:
  317.         
  318.                 Bit     Use
  319.                 ---     ---
  320.                 0       Read Only
  321.                 1       Hidden File
  322.                 2       System File
  323.                 3       Volume Name
  324.                 4       Directory
  325.                 5       Archive Bit
  326.  
  327.         By now, you should know how to test for these, but let's cover
  328.         it once more:
  329.         
  330.         IF attrib AND 1  THEN PRINT "Read Only File"
  331.         IF attrib AND 2  THEN PRINT "Hidden File
  332.         IF attrib AND 4  THEN PRINT "System File"
  333.         IF attrib AND 8  THEN PRINT "Volume Name"
  334.         IF attrib AND 16 THEN PRINT "Directory Name"
  335.         IF attrib AND 32 THEN PRINT "Archive File"
  336.         
  337.         If you feel daring, you can get this information yourself by
  338.         using Interrupt 21H, Function 43H, Subfunction 0.
  339.         Input:
  340.                 AH=43H
  341.                 AL=00H
  342.                 DS=Segment 
  343.                 DX=Offset
  344.                 
  345.                 DS:DX describes where to find the DOS file name.
  346.                       (File Name + Chr$(0).  See CHGMOD.BAS for
  347.                       a closely related example.)
  348.                         
  349.         OutPut: 
  350.                 Carry Flag =  0         OK
  351.                         CX =  File Attribute
  352.                      bit 1 =  Read Only
  353.                          2 =  Hidden
  354.                          3 =  System
  355.                          4 =  Volume Name           
  356.                          5 =  Archive Bit
  357.                 Carry Flag =  1         Error
  358.                         AX =  1  Unknown error
  359.                               2  File not found
  360.                               3  Path not found
  361.  
  362. ------------------
  363. 2.      INTERRUPTS
  364. ------------------
  365.  
  366.         The word for the day is "interrupt."  An interrupt is a signal to the
  367.         respective processor that its attention is required.  The operation
  368.         that is requesting the information returns a value that ranges from
  369.         0 to 255.  This value is known as the "type code."  The type code is
  370.         then used to calculate a new address known as an "interrupt vector."
  371.         This is the address of either a DOS or BIOS program that will deal with
  372.         the call.  Note, DOS stands for Disk Operating System and BIOS stands
  373.         for Basic Input/Output System - just in case you didn't know.  There
  374.         are some minor differences that exist between DR DOS that is marketed
  375.         by Novell and MS DOS that is by Microsoft (with version 5.00, I lump
  376.         IBM DOS and MS DOS together in terms of functionality, but this has
  377.         not always been the case).  I bring this up, because you must be
  378.         certain that your end-users can run your code.  If you make a system
  379.         call that only works in newer versions of DOS and not older ones, then
  380.         you should check for the DOS version BEFORE your program executes that
  381.         call.  If you don't you will potentially have some upset customers with
  382.         crashed systems.
  383.         Also, if you require a TSR (Terminate Stay Resident) program to be in
  384.         memory, such as LAN (Local Area Network) software, for an interrupt to
  385.         work, check for it BEFORE you call it!!!!
  386.  
  387.  
  388. ------------
  389. 3.      PEEK
  390. ------------
  391.  
  392.         PEEK is a command that resides in BASIC for the purpose of looking
  393.         at a byte in memory.  If you want to get the value of the first byte
  394.         of the BIOS communications table, you would do the following:
  395.  
  396.         DEF SEG = &H0
  397.         A = PEEK(&H400)
  398.         DEF SEG
  399.  
  400.         Line 1: This sets the current segment to &H0000 - the BIOS data area.
  401.                 (The start of the BIOS data area is actually 0000:0400).
  402.         Line 2: Reads the integer value that exists at &H400, one byte only.
  403.                 &H400 is called an "offset."
  404.         Line 3: This returns to the segment in which your BASIC programs is
  405.                 operating in.
  406.  
  407.         To PEEK word values, look at WORD.BAS.  BASIC's internal PEEK function
  408.         can only handle single bytes.  WORD.BAS contains sample code to PEEK
  409.         word values.
  410.  
  411. ------------
  412. 4.      Poke
  413. ------------
  414.  
  415.  
  416.         POKE places a value into memory at a specified address.  Whereas PEEK
  417.         reads the memory, POKE writes to the memory.  For example, let's say you
  418.         want the PC to forget that COM1 exists:
  419.  
  420.         DEF SEG = &H0
  421.         POKE &H400,&H0
  422.         POKE &H401,&H0
  423.         DEF SEG
  424.  
  425.         Line 1: This sets the current segment to &H0000, the BIOS segment.
  426.         Line 2: This writes the value &H0 to the memory location &H400.
  427.                 Remember, this location is called an offset.
  428.         Line 3: This writes the value &H0 to the offset &H401.
  429.         Line 4: Returns to the default BASIC segment.
  430.  
  431.         Voila!  No more COM1 !!
  432.  
  433.         To POKE words, see the WORD.BAS tutor file.  BASIC's POKE can only
  434.         do one byte at a time that is annoying.  WORD.BAS has sample code that
  435.         you can use to poke words into memory at any location.  POKEWORD could
  436.         have done the above to the comm port in one call.
  437.  
  438. ---------------
  439. 5.      SUMMARY
  440. ---------------
  441.         
  442.         ::FURTHER READING::
  443.  
  444.         There are two books that I recommend above all others:
  445.  
  446.         Tischer, Michael.  PC INTERN, Abacus, Grand Rapids, MI, 1992.
  447.                            ISBN:  1-55755-145-6
  448.  
  449.         Duncan, Ray.  The MS-DOS Encyclopedia, Microsoft Press,
  450.                       Redmond, WA, 1988.
  451.                       ISBN:  1-55615-174-8
  452.  
  453.         The encyclopedia is getting a little dated, but it is still useful.
  454.  
  455.         PC Intern is excellent!  Tons and tons of information and well worth
  456.         the $59.95.  It has over 1200 pages and includes sample code on a disk.
  457.  
  458. ------------------------
  459. 6.      Revision History
  460. ------------------------
  461.  
  462.         v1.1    May 2, 1993
  463.  
  464.                 Added:
  465.  
  466.                 CHGMOD.BAS    Tutor on obtaining file directores, setting the
  467.                               DTA address, and setting file attributes.
  468.  
  469.                 DUMPMEM.BAS   Tutor on using PEEK.  Runs from command line.
  470.                               (Functionally identical to READMEM.BAS)  
  471.                 
  472.                 PORT.BAS      Tutor on using the functions outlined in WORD.BAS
  473.                               to modify the I/O addresses in the BIOS data
  474.                               table.   
  475.  
  476.                 READMEM.BAS   Tutor on using PEEK.  Prompts user for answers.
  477.                               (Functionally identical to DUMPMEM.BAS)  
  478.  
  479.                 WORD.BAS      Contains functions for converting integers to
  480.                               long integers and PEEKing words, and a subroutine
  481.                               to POKE words.
  482.                               
  483.                 Modified:
  484.                 
  485.                 Revised S3BASTUT.DOC to reflect the new programs and corrected
  486.                 as many of the typographical errors as I could find.
  487.  
  488.         v1.0    April 20, 1993
  489.  
  490.                 Initial release.
  491.  
  492.  
  493. ----------------
  494. 7.      Register
  495. ----------------
  496.  
  497.         If you find this information useful, please consider registering it for
  498.         $10 US.  If there is interest, I will try to improve this tutorial.  At
  499.         the least, it served as an excellent review for me just to write it.
  500.         If you use sample code from the tutorial, please have the courtesy
  501.         to register the tutorial.  If you want the latest copy of the tutor
  502.         to be mailed to you with your registration, please add $5 to cover the
  503.         diskette and mailing (the total would then be ($10 x # copies)+ $5).
  504.  
  505.         Please send your registration to:
  506.  
  507.                 George Spafford
  508.                 3003 Lakeshore Drive, #216
  509.                 Saint Joseph, MI 49085
  510.                 USA
  511.  
  512.         I can be reached on the following BBS systems:
  513.  
  514.                 Michigan Online         19200-8-N-1     616-429-3414
  515.                 [This board is local to me and I try to call it at least
  516.                 twice a week - usually more on weekends.
  517.  
  518.                 EXEC-PC and Channel-1:  These are pay BBS systems, so I am
  519.                 not going to list their phone numbers.
  520.  
  521. ALL SAMPLE PROGRAMS ARE PART OF THE S3 SOFTWARE BASIC TUTORIAL.  THEY ARE NOT
  522. TO BE CONSIDERED SEPARATE.  IF YOU CHOOSE TO USE ANY OF THE PROGRAMS IN THIS
  523. PACKAGE AFTER A 30 DAY TRIAL PERIOD, YOU MUST REGISTER THE ENTIRE PACKAGE ON
  524. THE BASIS OF CONCURRENT USE.
  525.  
  526. NOTE:  The SDV file viewer is included with the tutorial for the use of viewing
  527. this .DOC file and other package files.  If you register this tutorial, you may
  528. continue to use it on a single system concurrently.  If you do not register
  529. this tutorial, but choose to continue using SDV, you must register it separately
  530. and details can be found in the SDV12.DOC file.
  531.  
  532. "Concurrent Use" defined:
  533.  
  534. I consider concurrent use to mean more than one PC running, or looking, at the
  535. tutorial and/or its programs at the same time.  If you plan on having 5 PCs
  536. using the tutor, or any of the programs that comprise it, at the same time, then
  537. you must register 5 copies of the program.
  538.  
  539. ============
  540.   LEGAL
  541. ============
  542.  
  543.  
  544. THE S3 BASIC TUTORIAL AND ALL OF ITS ACCOMPANYING SAMPLE CODE AND PROGRAMS ARE
  545. DISTRIBUTED AS IS.  THE AUTHOR (GEORGE SPAFFORD), OR ANY CONTRIBUTOR, MAKES NO
  546. WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  547. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, WITH 
  548. RESPECT TO THIS SOFTWARE AND DOCUMENTATION. IN NO EVENT SHALL THE AUTHOR OR
  549. CONTRIBUTORS BE LIABLE FOR ANY DAMAGES, INCLUDING LOST PROFITS, LOST SAVINGS,
  550. OR ANY OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF OR
  551. THE INABILITY TO USE THIS PROGRAM.
  552.  
  553. All products mentioned are the property of their respective companies.
  554.  
  555. SDV and the S3 BASIC Tutorial are the copyright of George Spafford.  All
  556. Rights Reserved.
  557.